Skip to main content

Action Creators

  • In practice, well-written Redux apps don't actually write those action objecs inline when we dispatch them (like dispatch({type: 'example', payload: payload})). Instead, we use action creator functions
  • An action creators is a function that creates and returns an action object. We use these so we don't have to write the action object by hand every time
const exampleActionCreator = text => {
return {
type: 'example',
payload: text
}
}

store.dispatch(exampleActionCreator("example text"))

Action Creators with Thunk Function

// Write a synchronous outer function that receives the `text` parameter:
export function outerFunction(arg) {
// And then creates and returns the async thunk function:
return async function thunkFunction(dispatch, getState) {
// ✅ Now we can use the arg (outsider variables)
...
dispatch(exampleActionCreator(arg))
}
}

// and then we can just call and dispatch the return value of the outerFunction
store.dispatch(outerFunction(someVariables))